//文章类别管理

const express = require('express')
const router = express.Router()
const db = require('../utils/db')



// 获取文章分类列表
router.get('/my/article/cates', (req, res) => {
    db(`SELECT * FROM category`, (err, result) => {
        if (err || result.length === 0) return res.send({ status: 1, message: '获取失败' })
        res.send({ status: 0, message: '获取文章分类列表成功！', data: result })
    })
})


//新增文章分类
router.post('/my/article/addcates', (req, res) => {
    const { name, alias } = req.body
    db(`INSERT INTO category SET name='${name}',alias='${alias}',is_delete=0`, (err, result) => {
        if (err) return res.send({ status: 1, message: '新增失败，请检查参数' })
        res.send({ status: 0, message: '新增文章分类成功！' })
    })
})



//根据 Id 删除文章分类
router.get('/my/article/deletecate/:id', (req, res) => {
    const { id } = req.params
    db(`UPDATE category SET is_delete=1 WHERE id = ${id}`, (err, result) => {
        if (err || result.affectedRows === 0) return res.send({ status: 1, message: '删除失败，请检查参数' })
        res.send({ status: 0, message: '删除文章分类成功！' })
    })
})


//根据 Id 获取文章分类数据
router.get('/my/article/cates/:id', (req, res) => {
    const { id } = req.params
    db(`SELECT * FROM category WHERE id = ${id}`, (err, result) => {
        if (err || result.length === 0) return res.send({ status: 1, message: '获取失败,请检查参数ID' })
        res.send({ status: 0, message: '获取文章分类成功！', data: result[0] })
    })
})


//根据 Id 更新文章分类数据
router.post('/my/article/updatecate', (req, res) => {
    const { id, name, alias } = req.body
    console.log({ id, name, alias })
    let sql = `UPDATE category SET name='${name}',alias='${alias}' WHERE id = ${id}`
    console.log(sql);
    db(sql, (err, result) => {
        console.log(err)
        if (err || result.changedRows === 0) return res.send({ status: 1, message: '更新失败,请检查参数ID' })
        res.send({ status: 0, message: '更新分类信息成功！' })
    })

})



module.exports = router